package com.maxd.modules.selfshop.merchants.respository;


import com.maxd.modules.selfshop.merchants.entity.SelfStore;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface SelfStoreJpaRepository extends JpaRepository<SelfStore, Long> {

    //条件查询带分页
    Page<SelfStore> findAll(Specification<SelfStore> specification, Pageable pageable);

    //条件查询
    List<SelfStore> findAll(Specification<SelfStore> specification);

    //根据商户id查询店铺
    @Query(value = "from SelfStore s where s.merchantId=:merchantId")
    SelfStore findByMerchantId(@Param("merchantId") Long merchantId);

    //检查店铺名称
    @Query(value = "from SelfStore s where s.storeName=:storeName")
    SelfStore checkStoreName(@Param("storeName") String storeName);

    //修改商户端账号状态
    @Modifying
    @Transactional
    @Query(value = "update SelfStore s set s.banStatus=:banStatus where s.storeId=:storeId")
    Integer banStatus(@Param("storeId") Long storeId, @Param("banStatus") Integer banStatus);

    //店铺评分减少
    @Modifying
    @Transactional
    @Query(value = "update SelfStore s set s.score=s.score-:score where s.storeId=:storeId")
    Integer lessScore(@Param("score") Integer score, @Param("storeId") Long storeId);

    //查询保证金到期的店铺
    @Modifying
    @Transactional
    @Query(value = "update  SelfStore s set s.marginStatus=1 where s.marginType = 2 and s.endTime<:endTime")
    Integer checkMargin(@Param("endTime") String endTime);

    //设置保证金到期
    @Modifying
    @Transactional
    @Query(value = "update SelfStore s set s.marginStatus=1 where s.storeId=:storeId")
    Integer updateMarginStatus(@Param("storeId") Long storeId);
}
